package com.liming.user.controller;

import com.liming.common.pojo.dto.cart.AddItemToCartDTO;
import com.liming.common.pojo.dto.cart.ItemInCartDTO;
import com.liming.common.pojo.vo.cart.web.ProductInCartVO;
import com.liming.common.pojo.vo.cart.web.UserCartVO;
import com.liming.common.result.LMResult;
import com.liming.user.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/22
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "购物车接口-Web")
@RequestMapping("/user/cart")
public class CartController {

    private final CartService cartService;

    @Operation(
            summary = "初始 - 查询用户购物车商品信息",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = ProductInCartVO.class))
                    })
            }
    )
    @GetMapping
    public LMResult<List<ProductInCartVO>> getProductInCart() {
        return LMResult.success(cartService.getProductInCart());
    }

    @Operation(
            summary = "查询 - 用户勾选购物车中商品",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = UserCartVO.class))
                    })
            }
    )
    @PostMapping("userCart")
    public LMResult<UserCartVO> userCartInfo(@RequestBody List<ItemInCartDTO> list) {
        return LMResult.success(cartService.userCartInfo(list));
    }

    @Operation(summary = "添加商品进入购物车")
    @PostMapping("addItem")
    public LMResult<Boolean> addCart(@RequestBody AddItemToCartDTO dto) {
        return LMResult.success(cartService.addItemToCart(dto));
    }

    @Operation(summary = "减少购物车中商品数量")
    @PutMapping("reduceItem/{id}")
    public LMResult<Boolean> reduceItem(@PathVariable Long id) {
        return LMResult.success(cartService.reduceItemById(id));
    }

    @Operation(summary = "增加购物车中商品数量")
    @PutMapping("addItem/{id}")
    public LMResult<Boolean> addItem(@PathVariable Long id) {
        return LMResult.success(cartService.addItemById(id));
    }

    @Operation(summary = "删除购物车中商品")
    @DeleteMapping("removeItems")
    public LMResult<Boolean> deleteItems(@RequestBody List<Long> ids) {
        return LMResult.success(cartService.deleteItemsByIds(ids));
    }
}
